document.addEventListener('DOMContentLoaded', async () => {
  // 获取配置数据
  const config = await chrome.storage.local.get(['youdaoAppKey', 'youdaoSecret', 'tigerDeckApiKey']);
  const { youdaoAppKey = '', youdaoSecret = '', tigerDeckApiKey = '' } = config;

  // 设置输入框的值
  const appKeyInput = document.getElementById('youdaoAppKey');
  const secretInput = document.getElementById('youdaoSecret');
  const tigerDeckApiKeyInput = document.getElementById('tigerDeckApiKey');
  
  appKeyInput.value = youdaoAppKey;
  secretInput.value = youdaoSecret;
  tigerDeckApiKeyInput.value = tigerDeckApiKey;

  // 保存配置
  const saveButton = document.getElementById('saveApiKey');
  const saveStatus = document.getElementById('saveStatus');
  
  saveButton.addEventListener('click', async () => {
    const appKey = appKeyInput.value.trim();
    const secret = secretInput.value.trim();
    const tigerDeckKey = tigerDeckApiKeyInput.value.trim();
    
    if (!appKey || !secret) {
      showStatus('请输入应用ID和应用密钥', 'error');
      return;
    }
    
    try {
      await chrome.storage.local.set({ 
        youdaoAppKey: appKey,
        youdaoSecret: secret,
        tigerDeckApiKey: tigerDeckKey
      });
      showStatus('保存成功', 'success');
    } catch (error) {
      showStatus('保存失败: ' + error.message, 'error');
    }
  });
  
  // 显示状态信息
  function showStatus(message, type) {
    saveStatus.textContent = message;
    saveStatus.className = type;
    
    // 3秒后清除状态信息
    setTimeout(() => {
      saveStatus.textContent = '';
      saveStatus.className = '';
    }, 3000);
  }
});